package com.space.school.controller;

import com.space.school.domain.Detail;
import com.space.school.domain.Info;
import com.space.school.service.InfoService;
import com.space.school.service.impl.DetailServiceImpl;
import com.space.school.service.impl.InfoServiceImpl;
import com.space.school.utils.KeyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "detail",value = "/detail")
public class DetailServlet extends HttpServlet {
    private InfoService infoService=new InfoServiceImpl();

    private DetailServiceImpl detailService=new DetailServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Info> info=infoService.findAll();
        req.setAttribute("info",info);
        req.getRequestDispatcher("detail.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String action=req.getParameter("action");
        String order_id=req.getParameter("order_id");
        String product_id=req.getParameter("product_id");
        String product_num=req.getParameter("product_num");
        if(order_id.equals("") || order_id==null){
            order_id= KeyUtil.generateKey();
        }
        if(action.equals("del")){
//            退出(删除)操作
            detailService.del(order_id);
            req.getRequestDispatcher("list.jsp").forward(req, resp);
        }else if(action.equals("add")){
//            继续添加操作
            Detail detail=new Detail();
            detail.setOrder_id(order_id);
            detail.setProduct_id(product_id);
            detail.setProduct_num(Integer.parseInt(product_num));

            boolean flag=detailService.insert(detail);
            req.setAttribute("order_id",order_id);
            if (!flag) {
                req.setAttribute("detail_message", "您的点单超过库存了,请重新点餐");
            }
            List<Info> info=infoService.findAll();
            req.setAttribute("info",info);
            req.getRequestDispatcher("detail.jsp").forward(req,resp);
        }else if(action.equals("next")){
//            下一步操作,直接跳转界面
            Detail detail=new Detail();
            detail.setOrder_id(order_id);
            detail.setProduct_id(product_id);
            detail.setProduct_num(Integer.parseInt(product_num));
            boolean flag=detailService.insert(detail);
            req.setAttribute("order_id",order_id);
            if (!flag) {
                req.setAttribute("detail_message", "您的点单超过库存了,请重新点餐");
                List<Info> info=infoService.findAll();
                req.setAttribute("info",info);
                req.getRequestDispatcher("detail.jsp").forward(req, resp);
            }else {
                req.setAttribute("order_sum", detailService.find_sum(Long.valueOf(order_id)).get(0));
                req.getRequestDispatcher("master.jsp").forward(req, resp);
            }
        }

    }
}
